//Component Object
const { formatSeconds }  = require('../../utils/util')

Component({
  options: {
    pureDataPattern: /^_/ // 指定所有 _ 开头的数据字段为纯数据字段
  },
  pageLifetimes:{
    hide:function(){
      let that = this as any
      that.pauseThisAudio()
    }
  },
  properties: {
    audioUrl:{
      type:String,
      value:'',
    },
    voiceDuration:{
      type:Number,
      value:0
    },
    playingAudioUrl:{
      type:String,
      value:'',
      observer: function(newValue:string){
        let that = this as any
        if(newValue !== that.data.audioUrl){
          that.pauseThisAudio()
        }
      }
    },
    index:{
      type:Number,
    },
    curSwiperIndex:{
      type:Number,
      observer: function(newValue:number){
        // console.log('curSwiperIndex:',newValue)
        let that = this as any
        if(newValue == that.data.index){
          // 只有5个以内，才初始化播放器，避免初始化过多播放器
          // console.log('初始化播放器：',this.data.index)
          that.initAudioContext()
        }else{
          that.destoryAudioContext()
        }
      
      }
    }
  },
  data: {
    playStatus:'pause',
    _audioContext:null,
    progress:0,
    currentTime:'00:00',
    duration:'00:00',
    _dragCurrentTime:0
  },
  methods: {
    sliderDrag(event:any){
      // console.log('sliderDrag',event)
      let value = event.detail.value
      let second = value * this.data.voiceDuration / 100
      // this.data._audioContext.seek(second)
      // console.log('sliderDrag second:',second)
      this.setData({
        _dragCurrentTime:second
      })
    },
    sliderDragEnd(){
      if(this.data._audioContext){
        this.data._audioContext.pause()
        setTimeout(()=>{
          // console.log('sliderDragEnd',this.data._dragCurrentTime)
          this.data._audioContext.seek(this.data._dragCurrentTime)
          this.playThisAudio()
        },200)
      }else {

      }
      
      
    },
    changePlayStstus(status:string){
      let that = this as any
      console.log('changePlayStstus that:',that,status)
      that.setData({
        playStatus:status
      },()=>{
        console.log('that:',that.data.playStatus)
      })
    }, 
    initAudioContext(){
      // console.log('initAudioContext')
      if(this.data._audioContext){
        return 
      }
      let that = this as any
      let _audioContext = wx.createInnerAudioContext({}) as any
      // 当前音频可播放
      _audioContext.onCanplay(()=>{
        // console.log('onCanplay _audioContext',_audioContext)
        let duration = _audioContext.duration
        // console.log('onCanplay _audioContext.duration',_audioContext.duration)
        // that.setData({
        //   duration:formatSeconds(_audioContext.duration)
        // })
      })
      _audioContext.onPlay(()=>{
        console.log('onPlay')
        // console.log('onPlay _audioContext',_audioContext)
        console.log('onPlay _audioContext.duration',_audioContext.duration)
        // console.log('onCanplay that：',that)
        // console.log('audioContext2:',audioContext)
        that.setData({
          playStatus:'play'
        })
      })
      _audioContext.onSeeked((event:any)=>{
        console.log('onSeeked event',event)
        // console.log('onSeeked _audioContext',_audioContext)
        // console.log('onCanplay that：',that)
        // console.log('audioContext2:',audioContext)
        // that.playThisAudio()
      })
   
      _audioContext.onStop(()=>{
        console.log('stop')
        that.setData({
          playStatus:'pause'
        })
      })
      _audioContext.onEnded(()=>{
        console.log('onEnded')
        that.setData({
          playStatus:'pause',
          progress:0,
          currentTime:'00:00'
        })
        _audioContext.seek(0)
        _audioContext.pause()
        
        
      })

      _audioContext.onError((err:any)=>{
        _audioContext.stop()
        console.log('播放错误：',err)
        // wx.showToast({
        //   title:'播放错误',
        //   duration:3000
        // })
        that.setData({
          playStatus:'pause',
          _audioContext: null
        })
        
      })

      _audioContext.onTimeUpdate(()=>{
        // console.log('onTimeUpdate:',_audioContext.currentTime)
        that.setData({
          currentTime:formatSeconds(_audioContext.currentTime),
          progress:(_audioContext.currentTime / that.data.voiceDuration ) * 100
        })
      })
      // 初始化音频
      _audioContext.src = that.data.audioUrl

      that.setData({
        _audioContext,
      })
    },
    playThisAudio(){
      let that = this as any
      // if(!that.data._audioContext){
      //   that.initAudioContext()
      // }
      if(that.data._audioContext){
        that.data._audioContext.play()
        console.log('playThisAudio')
        that.triggerEvent('parentPlayThisAudio',{audioUrl: that.data.audioUrl})
      }
    },
    pauseThisAudio(){
      let that = this as any
      // console.log('pauseThisAudio ',that.data.audioId)
      if(that.data._audioContext){
        that.data._audioContext.pause()
        
      }
      (this as any).setData({
        playStatus:'pause'
      })
    },
    destoryAudioContext(){
      let that = this as any
      if(that.data._audioContext){
        that.pauseThisAudio()
        that.data._audioContext.destroy()
        that.data._audioContext = null
      }
    }  
  },
  
  attached:function(){
    // this.initAudioContext()
    // console.log('attached:',this.data.curSwiperIndex)
    this.setData({
      duration:formatSeconds(this.data.voiceDuration)
    })
  },
  detached:function(){
    this.destoryAudioContext()
  }
});